package cn.iocoder.yudao.module.common.controller.admin.knowledgepointrelation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.knowledgepointrelation.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.knowledgepointrelation.KnowledgePointRelationDO;
import cn.iocoder.yudao.module.common.service.knowledgepointrelation.KnowledgePointRelationService;

@Tag(name = "管理后台 - 知识关系点")
@RestController
@RequestMapping("/common/knowledge-point-relation")
@Validated
public class KnowledgePointRelationController {

    @Resource
    private KnowledgePointRelationService knowledgePointRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建知识关系点")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point-relation:create')")
    public CommonResult<Long> createKnowledgePointRelation(@Valid @RequestBody KnowledgePointRelationSaveReqVO createReqVO) {
        return success(knowledgePointRelationService.createKnowledgePointRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新知识关系点")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point-relation:update')")
    public CommonResult<Boolean> updateKnowledgePointRelation(@Valid @RequestBody KnowledgePointRelationSaveReqVO updateReqVO) {
        knowledgePointRelationService.updateKnowledgePointRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识关系点")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:knowledge-point-relation:delete')")
    public CommonResult<Boolean> deleteKnowledgePointRelation(@RequestParam("id") Long id) {
        knowledgePointRelationService.deleteKnowledgePointRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识关系点")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point-relation:query')")
    public CommonResult<KnowledgePointRelationRespVO> getKnowledgePointRelation(@RequestParam("id") Long id) {
        KnowledgePointRelationDO knowledgePointRelation = knowledgePointRelationService.getKnowledgePointRelation(id);
        return success(BeanUtils.toBean(knowledgePointRelation, KnowledgePointRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得知识关系点分页")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point-relation:query')")
    public CommonResult<PageResult<KnowledgePointRelationRespVO>> getKnowledgePointRelationPage(@Valid KnowledgePointRelationPageReqVO pageReqVO) {
        PageResult<KnowledgePointRelationDO> pageResult = knowledgePointRelationService.getKnowledgePointRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgePointRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出知识关系点 Excel")
    @PreAuthorize("@ss.hasPermission('common:knowledge-point-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportKnowledgePointRelationExcel(@Valid KnowledgePointRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KnowledgePointRelationDO> list = knowledgePointRelationService.getKnowledgePointRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "知识关系点.xls", "数据", KnowledgePointRelationRespVO.class,
                        BeanUtils.toBean(list, KnowledgePointRelationRespVO.class));
    }

}